package com.expriment.aes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class WRFile {

    public void writeENIntoFile(double extraNoise) throws IOException {
        File file = new File("D://extraNoise.txt"); // 存放数组数据的文件
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter out = new FileWriter(file); // 文件写入流
        out.write(String.valueOf(extraNoise));
        out.close();
    }

    public double readENFromFile() throws NumberFormatException, IOException {
        BufferedReader in = new BufferedReader(new FileReader("D://extraNoise.txt"));
        double extraNoise = Double.parseDouble(in.readLine());
        in.close();
        return extraNoise;
    }

    public void writeKeysIntoFile(double[][] keys) throws IOException {
        File file = new File("D://keys.txt"); // 存放数组数据的文件
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter out = new FileWriter(file); // 文件写入流
        int row = keys.length;
        // 将数组中的数据写入到文件中。每行各数据之间TAB间隔
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < 2; j++) {
                out.write(keys[i][j] + "\t");
            }
            out.write("\r\n");
        }
        out.close();
    }

    public double[][] readKeysFromFile(int encNum) throws NumberFormatException, IOException {
        BufferedReader in = new BufferedReader(new FileReader("D://keys.txt"));
        String line; // 一行数据
        double[][] keys = new double[encNum][2];
        // 逐行读取，并将每个数组放入到数组中
        int row = 0;
        while ((line = in.readLine()) != null) {
            String[] temp = line.split("\t");
            for (int j = 0; j < 2; j++) {
                keys[row][j] = Double.parseDouble(temp[j]);
            }
            row++;
        }
        in.close();
        return keys;
    }
}